package com.jumi.microservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.jumi.microservice.common.constant.ImageResizeEnum;
import com.jumi.microservice.common.exception.BusinessException;
import com.jumi.microservice.common.redis.RedisCache;
import com.jumi.microservice.common.utils.ImageResizeUtil;
import com.jumi.microservice.constant.FavoritesEnum;
import com.jumi.microservice.constant.GoodsKeyConstant;
import com.jumi.microservice.constant.NumberConstant;
import com.jumi.microservice.domain.GoodsDO;
import com.jumi.microservice.domain.GoodsFavoritesDO;
import com.jumi.microservice.domain.GoodsFavoritesDTO;
import com.jumi.microservice.mapper.GoodsFavoritesMapper;
import com.jumi.microservice.mapper.GoodsMapper;
import com.jumi.microservice.service.GoodsFavoritesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * 商品收藏管理service接口
 *
 * @author Administrator
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class GoodsFavoritesServiceImpl implements GoodsFavoritesService {

    /**
     * 收藏商品管理mapper组件
     */
    @Autowired
    private GoodsFavoritesMapper goodsFavoritesMapper;

    /**
     * 商品管理mapper组件
     */
    @Autowired
    private GoodsMapper goodsMapper;

    @Autowired
    private RedisCache redisCache;

    /**
     * 收藏商品
     *
     * @param memberId 会员ID
     * @param goodsId  收藏商品ID
     */
    @Override
    public void addServeGoods(Long memberId, Long goodsId) {
        GoodsDO goodsDO = goodsMapper.selectById(goodsId);

        if (Objects.isNull(goodsDO)) {
            throw new BusinessException(FavoritesEnum.FAVORITES_GOODS_NOT_EXISTED);
        }

        LambdaQueryWrapper<GoodsFavoritesDO> goodsFavoritesQueryWrapper = Wrappers.lambdaQuery();
        goodsFavoritesQueryWrapper.eq(GoodsFavoritesDO::getGoodsId, goodsId);
        goodsFavoritesQueryWrapper.eq(GoodsFavoritesDO::getMemberId, memberId);

        Integer integer = goodsFavoritesMapper.selectCount(goodsFavoritesQueryWrapper);
        if (integer == 0) {
            GoodsFavoritesDO goodsFavorites = new GoodsFavoritesDO();
            goodsFavorites.setMemberId(memberId);
            goodsFavorites.setGoodsId(goodsId);
            goodsFavoritesMapper.insert(goodsFavorites);
        }
    }

    /**
     * 批量收藏商品
     *
     * @param memberId 会员ID
     * @param goodsIds 收藏商品ID
     */
    @Override
    public void batchAddServeGoods(Long memberId, List<Long> goodsIds) {
        if (goodsIds != null) {
            for (Long goodsId : goodsIds) {
                GoodsDO goodsDO = goodsMapper.selectById(goodsId);

                if (Objects.isNull(goodsDO)) {
                    throw new BusinessException(FavoritesEnum.FAVORITES_GOODS_NOT_EXISTED);
                }

                LambdaQueryWrapper<GoodsFavoritesDO> goodsFavoritesQueryWrapper = Wrappers.lambdaQuery();
                goodsFavoritesQueryWrapper.eq(GoodsFavoritesDO::getGoodsId, goodsId);
                goodsFavoritesQueryWrapper.eq(GoodsFavoritesDO::getMemberId, memberId);

                Integer integer = goodsFavoritesMapper.selectCount(goodsFavoritesQueryWrapper);
                if (integer == 0) {
                    GoodsFavoritesDO goodsFavorites = new GoodsFavoritesDO();
                    goodsFavorites.setMemberId(memberId);
                    goodsFavorites.setGoodsId(goodsId);
                    goodsFavoritesMapper.insert(goodsFavorites);
                }
            }
        }
    }

    /**
     * 取消收藏商品
     *
     * @param memberId 会员ID
     * @param goodsId  收藏商品ID
     */
    @Override
    public void cancelServeGoods(Long memberId, Long goodsId) {
        LambdaQueryWrapper<GoodsFavoritesDO> goodsFavoritesQueryWrapper = Wrappers.lambdaQuery();
        goodsFavoritesQueryWrapper.eq(GoodsFavoritesDO::getGoodsId, goodsId);
        goodsFavoritesQueryWrapper.eq(GoodsFavoritesDO::getMemberId, memberId);
        goodsFavoritesMapper.delete(goodsFavoritesQueryWrapper);
    }

    /**
     * 列表展现收藏的商品
     *
     * @param memberId 会员id
     * @return 返回结果
     */
    @Override
    public List<GoodsFavoritesDTO> listServeGoods(Long memberId) {
        LambdaQueryWrapper<GoodsFavoritesDO> goodsFavoritesQueryWrapper = Wrappers.lambdaQuery();
        goodsFavoritesQueryWrapper.eq(GoodsFavoritesDO::getMemberId, memberId);
        goodsFavoritesQueryWrapper.orderByDesc(GoodsFavoritesDO::getFavTime);
        List<GoodsFavoritesDO> goodsFavoritesList = goodsFavoritesMapper.selectList(goodsFavoritesQueryWrapper);

        List<GoodsFavoritesDTO> goodsFavoritesDtoList = new ArrayList<>(goodsFavoritesList.size());
        for (GoodsFavoritesDO goodsFavorites : goodsFavoritesList) {
            GoodsDO goods = goodsMapper.selectById(goodsFavorites.getGoodsId());

            if (Objects.isNull(goods)) {
                continue;
            }
            GoodsFavoritesDTO goodsFavoritesDto = new GoodsFavoritesDTO();
            goodsFavoritesDto.setId(goodsFavorites.getId());
            goodsFavoritesDto.setMemberId(goodsFavorites.getMemberId());
            goodsFavoritesDto.setGoodsId(goodsFavorites.getGoodsId());
            goodsFavoritesDto.setGoodsName(goods.getGoodsName());

            Object cacheObject = redisCache.getCacheObject(GoodsKeyConstant.POSTAGEGOODS + goods.getId());
            if (!Objects.isNull(cacheObject)) {
                goodsFavoritesDto.setGoodsPrice(BigDecimal.ZERO);
            } else {
                goodsFavoritesDto.setGoodsPrice(goods.getGoodsPrice());
            }

            goodsFavoritesDto.setGoodsImage(ImageResizeUtil.imageResize(goods.getGoodsImage(), ImageResizeEnum.LIST));
            goodsFavoritesDto.setFavTime(goodsFavorites.getFavTime());
            goodsFavoritesDto.setGoodsDescription(goods.getGoodsDescription());
            goodsFavoritesDto.setGoodsMarketprice(goods.getGoodsMarketprice());
            goodsFavoritesDtoList.add(goodsFavoritesDto);
        }
        return goodsFavoritesDtoList;
    }

    /**
     * 查询商品是否收藏
     *
     * @param apiUserId 用户ID
     * @param goodsId   商品spuid
     * @return 返回结果
     */
    @Override
    public boolean isGoodsFavorites(Integer apiUserId,Long goodsId) {
        LambdaQueryWrapper<GoodsFavoritesDO> goodsFavoritesQueryWrapper = Wrappers.lambdaQuery();
        goodsFavoritesQueryWrapper.eq(GoodsFavoritesDO::getMemberId,apiUserId.longValue());
        goodsFavoritesQueryWrapper.eq(GoodsFavoritesDO::getGoodsId, goodsId);
        List<GoodsFavoritesDO> goodsFavoritesList = goodsFavoritesMapper.selectList(goodsFavoritesQueryWrapper);

        if (goodsFavoritesList != null) {
            return goodsFavoritesList.size() > NumberConstant.NUMBER_ZERO.intValue();
        }
        return false;
    }
}
